package components;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

import utils.Utils;

/**
 * 
 * make a button with rounded shape
 *
 */
public class RoundButton extends JComponent {

	private static final long serialVersionUID = 1L;
	
	private static final Color FILL_COLOUR = new Color (255, 255, 255, 180);
	private static final Color BACKGROUND_STROKE_COLOUR = new Color (100, 100, 100);
	private static final int DEFAULT_CORNER_SIZE = 5;

	private int    width;
	private int    height;
	private int    corner;
	private Color  strokeColor;
	private Color  fillColor;
	private String text;

	public RoundButton (String text , int width , int height) {

		this(text, width, height, DEFAULT_CORNER_SIZE, BACKGROUND_STROKE_COLOUR, FILL_COLOUR);
	}
	
	public RoundButton (String text , int width , int height , int corner , Color strokeColor , Color fillColor) {
		this.width = width;
		this.height = height;
		this.corner = corner;
		this.strokeColor = strokeColor;
		this.fillColor = fillColor;
		this.text = text;
		setPreferredSize(new Dimension(width, height));
		setVisible(true);
	}
	
	@Override
	public void paintComponent (Graphics g) {
		super.paintComponent (g);
		
		Graphics2D g2 = (Graphics2D)g;
		
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 
		
		// Draw the button
		g2.setColor(fillColor);
		g2.fillRoundRect(0, 0, width, height, corner, corner);
		g2.setColor(strokeColor);
		g2.drawRoundRect(0, 0, width-1, height-1, corner, corner);
		
		// Draw the text inside the button
		g2.setColor(Color.RED);
		g.setFont(new Font("David", Font.BOLD, 22));

		Rectangle2D r2d = Utils.getStringBounds(text, g);

		g2.drawString(text,(int) ((width - r2d.getWidth()) /2) ,(int) ((height - r2d.getHeight()) /2));
	}

	public String getText() {
		return text;
	}
}	
